package com.example.webflux_module.controller;

import com.example.webflux_module.entity.Customer;
import com.example.webflux_module.service.CustomerService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping("/customers")
public class CustomerController {
    private final CustomerService service;

    public CustomerController(CustomerService service) {
        this.service = service;
    }

    @GetMapping
    public Flux<Customer> getAllCustomers() {
        return service.getAllCustomers();
    }

    @GetMapping("/{id}")
    public Mono<ResponseEntity<Customer>> getCustomerById(@PathVariable Long id) {
        return service.getCustomerById(id)
                .map(customer -> ResponseEntity.ok(customer))
                .defaultIfEmpty(ResponseEntity.ok(Customer.builder().build()));
    }

    @PostMapping
    public Mono<Customer> createCustomer(@RequestBody Customer customer) {
        return service.createCustomer(customer);
    }

    // Update and delete endpoints...
}
